/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jpa.controllers;

import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entity.Coursetag;
import entity.Tagrates;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import jpa.controllers.exceptions.NonexistentEntityException;

/**
 *
 * @author atap
 */
public class TagratesJpaController implements Serializable {

    public TagratesJpaController(){
        
    }
    
    public TagratesJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("EducationXPU");

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Tagrates tagrates) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Coursetag courseTagId = tagrates.getCourseTagId();
            if (courseTagId != null) {
                courseTagId = em.getReference(courseTagId.getClass(), courseTagId.getId());
                tagrates.setCourseTagId(courseTagId);
            }
            em.persist(tagrates);
            if (courseTagId != null) {
                courseTagId.getTagratesList().add(tagrates);
                courseTagId = em.merge(courseTagId);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Tagrates tagrates) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Tagrates persistentTagrates = em.find(Tagrates.class, tagrates.getId());
            Coursetag courseTagIdOld = persistentTagrates.getCourseTagId();
            Coursetag courseTagIdNew = tagrates.getCourseTagId();
            if (courseTagIdNew != null) {
                courseTagIdNew = em.getReference(courseTagIdNew.getClass(), courseTagIdNew.getId());
                tagrates.setCourseTagId(courseTagIdNew);
            }
            tagrates = em.merge(tagrates);
            if (courseTagIdOld != null && !courseTagIdOld.equals(courseTagIdNew)) {
                courseTagIdOld.getTagratesList().remove(tagrates);
                courseTagIdOld = em.merge(courseTagIdOld);
            }
            if (courseTagIdNew != null && !courseTagIdNew.equals(courseTagIdOld)) {
                courseTagIdNew.getTagratesList().add(tagrates);
                courseTagIdNew = em.merge(courseTagIdNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = tagrates.getId();
                if (findTagrates(id) == null) {
                    throw new NonexistentEntityException("The tagrates with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Tagrates tagrates;
            try {
                tagrates = em.getReference(Tagrates.class, id);
                tagrates.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The tagrates with id " + id + " no longer exists.", enfe);
            }
            Coursetag courseTagId = tagrates.getCourseTagId();
            if (courseTagId != null) {
                courseTagId.getTagratesList().remove(tagrates);
                courseTagId = em.merge(courseTagId);
            }
            em.remove(tagrates);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Tagrates> findTagratesEntities() {
        return findTagratesEntities(true, -1, -1);
    }

    public List<Tagrates> findTagratesEntities(int maxResults, int firstResult) {
        return findTagratesEntities(false, maxResults, firstResult);
    }

    private List<Tagrates> findTagratesEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Tagrates.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Tagrates findTagrates(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Tagrates.class, id);
        } finally {
            em.close();
        }
    }

    public int getTagratesCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Tagrates> rt = cq.from(Tagrates.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
